<?php

class IndexController extends St_Controller_Action {

    public function indexAction() {
    }
    
    public function editinformationAction() {
        $token = $this->_getParam("param1", false);
        $uid = $this->_getParam("param2", false);
        $canAdd = true;
        $result = false;
        $baseUrl = St_Registry::getConfig("base_url");
        $this->view->headLink()
                ->appendStylesheet($baseUrl . 'application/admin/externals/css/prettyPhoto.css');
        $this->view->headScript()
                ->appendFile($baseUrl . 'application/admin/externals/js/jquery.prettyPhoto.js');
        $this->view->baselink = St_Registry::getConfig("base_url");
        $this->view->mailicon = $baseUrl . "application/admin/externals/images/mail_icon.png";
        $this->view->smsicon = $baseUrl . "application/admin/externals/images/sms_icon.png";

        if (!$uid) {
            $this->_redirect("user");
        }
        if ($_SESSION['userSchool'] != '') {
            $userObj = St_Registry::getDB();
            $datas = $userObj->fetchAll("SELECT * from relativetypes where SchoolID=" . $_SESSION['userSchool']);
            $this->view->Relativetype = $datas;
        } else {
            $userObj = St_Registry::getDB();
            $temp = $userObj->query("SELECT RelativeID, SchoolID  from relative where RelativeID=" . $uid);
            $schoolid = $temp->fetchColumn(1);
            $datas = $userObj->fetchAll("SELECT * from relativetypes where SchoolID=" . $schoolid);
            $this->view->Relativetype = $datas;
        }
        $schoolObjc = St_Registry::getDB();
        $datasc = $schoolObjc->fetchAll("SELECT * from st_countries");
        ;
        $this->view->countrylist = $datasc;
        $schoolObj = St_Registry::getDB();
        $datas = $schoolObj->fetchAll("SELECT * from school_list");
        ;
        $this->view->schoolObj = $datas;
        $this->view->dataId = $uid;
        Zend_Loader::loadClass("St_Relative");
        $siteObj = new St_Relative();
        $currentControllerName = St_Registry::getControllerName();
        $db = St_Registry::getDB();
        $select = $db->select()
                ->from(array('u' => 'relative'))
                ->where("RelativeID  = '$uid'");
        $arrayRow = $db->fetchRow($select);
        if ($arrayRow['Token'] != $token) {
            echo "ERROR: Token code is invalid";
            die;
        }

        $select2 = $db->select()
                ->from(array('u' => 'studentrelative'))
                ->joinLeft(array('g' => 'student'), 'g.StudentID = u.StudentID')
                ->where("u.RelativeID=" . $uid)
        ;
        $adapter2 = new Zend_Paginator_Adapter_DbSelect($select2);
        $paginator = new Zend_Paginator($adapter2);

        $paginator->setCurrentPageNumber($this->_getParam("page", 1));
        $paginator->setItemCountPerPage(30);
        //$paginator->setPageRange ( 5 );
        $this->view->paginator = $paginator;
        if (count($paginator) && !empty($paginator)) {
            $stuid = "(";
            foreach ($paginator as $v) {
                //echo $v['StudentID']."<br>";
                $stuid.=$v['StudentID'] . ",";
            }
            $stuid.="0)";
            //echo $stuid;
            $select3 = $db->select()
                    ->from(array('u' => 'relative'))
                    ->joinLeft(array('g' => 'studentrelative'), 'g.RelativeID = u.RelativeID')
                    ->where("g.StudentID IN " . $stuid)
                    ->where("u.RelativeID !=" . $uid)
                    ->group('u.RelativeID');
            ;
            // ->from ( array('u' => 'studentrelative' ),'u.RelativeID')
            //  ->joinLeft(array('g' => 'relative'), 'g.RelativeID = u.RelativeID')
            $adapter3 = new Zend_Paginator_Adapter_DbSelect($select3);
            $paginator2 = new Zend_Paginator($adapter3);
            $this->view->paginator2 = $paginator2;
        }

        if ($arrayRow) {
            $this->view->data = $arrayRow;
        }

        $data = $this->_getParam("data", false);
        $data2 = $this->_getParam("data2", false);
        Zend_Loader::loadClass("St_School");
        $schoolObj = new St_School();
        $isAuto = $schoolObj->getIsAutoIncre();
        $this->view->isAuto = $isAuto;
        Zend_Loader::loadClass("St_Relative");
        $relativeObj = new St_Relative();
        $maxClientNumber = $relativeObj->getMaxClientNumber($arrayRow['SchoolID']);
        $this->view->maxClientNumber = intval($maxClientNumber['NextNumber']) + 1;

        if ($data) {

            if (($data['FirstName'] == "") || (($data['LastName'] == "")) || (($data['Email'] == "")) || (($data['CountryID'] == ""))) {
                $canAdd = false;
            }


            if ($canAdd) {
                $BirthDate = $data2['day'] . "-" . $data2['month'] . "-" . $data2['year'];
                $data['Birthdate'] = strftime("%Y-%m-%d", strtotime($BirthDate));
                //$data['Birthdate']=strftime("%Y-%m-%d", strtotime($data['Birthdate']));
                $select1 = $db->select()
                        ->from(array('u' => 'relative'), 'Token')
                        ->where("RelativeID = {$uid}")
                ;
                $lastToken = $db->fetchRow($select1);
                $lastToken = $lastToken['Token'];
                if ($lastToken != $data['Token']) {
                    $Token = $data['Token'];
                    while (true) {
                        $select1 = $db->select()
                                ->from(array('u' => 'relative'))
                                ->where("Token = {$Token}")
                        ;
                        if ($db->fetchRow($select1)) {
                            $Token = St_Registry::generateRandomNumber(8);
                        } else {
                            if (strlen($Token) == 8)
                                break;
                        }
                    }
                    $data['Token'] = $Token;
                }

                $result = $siteObj->update($data, "RelativeID = '$uid'");
            }

            if ($result) {
                $user = $this->_getCurrentUser();
                $this->logger->log($user['UserName'] . " edits RelativeID =" . $uid);
                $this->view->msg = "Update Releative successfully";
            } else {
                $this->view->msg = "Can't update Releative";
            }
            $this->view->data = $data;
//            $this->_redirect("{$currentControllerName}/relative");
        }

        // Binh Add: 7-11-2011 get school external setting
        $SchoolID = $arrayRow['SchoolID'];
        $select = $db->select()
                ->from(array('u' => 'school_list'))
                ->where("SchoolID = '$SchoolID'");
        $schoolInfo = $db->fetchRow($select);
        $ExternalBrowserTitle = $schoolInfo['ExternalBrowserTitle'];
        if ($ExternalBrowserTitle) {
            $this->view->headTitle($ExternalBrowserTitle);
        } else {
            $this->view->headTitle('School System');
        }
        $this->view->schoolInfo = $schoolInfo;
    }

}

